﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using smartBrowser.Util;
using System.Windows.Forms;
using SemWeb;
using smartBrowser.GUI;
using smartBrowser.Ontology;

namespace smartBrowser.Task
{
    public class LearnLocalOntologyTask : Task
    {
        private Hashtable ontologies;
        private Ontology.Ontology localOntology = smartBrowser.GUI.SmartBrowser.Instance.Ontologia;

        public LearnLocalOntologyTask(WebSite webSite, TabPageForm form, Hashtable ontologyList)
            : base(webSite, form)
        {
            this.ontologies = ontologyList;
        }

        protected override void Execute()        
        {
            Console.Out.WriteLine("Learn Local Ontology Task. Has "+this.ontologies.Count+" to process");
            foreach (String concept in this.ontologies.Keys)
            {
                MemoryStore onto = (MemoryStore)this.ontologies[concept.ToLower()];
                
                mergeOntology(onto, concept);
            }
            
            FireCompleteEvent();
            Console.Out.WriteLine("LearnLocalOntology Task Complete");
        }

        public void getSuperClass(Entity entity, MemoryStore ontology)
        {
            SelectResult result = ontology.Select(new Statement(entity, (Entity)"http://www.w3.org/2000/01/rdf-schema#subClassOf", null));
            MemoryStore store = result.Load();

            Console.Out.WriteLine("Ontology Count: " + ontology.StatementCount + " result counts: " + store.StatementCount);
            
            //PAra cada statement na ontologia
            foreach (Statement statement in store)
            {
                Console.Out.WriteLine("Subject: " + statement.Subject.Uri + " Predicate: " + statement.Predicate.Uri + " Object: " + statement.Object.Uri);

                //Se a entidade contém o conceito (ou seja, se o conceito está definido através desta entidade)
                if (this.localOntology.Ontologia.ContainsKey(((Tag)Ontology.Ontology.parseUri(statement.Object.Uri)).Concept.ToLower()))
                {
                    Console.Out.WriteLine("Learn Ontology: Apanhei-te!!");

                }
                else
                {
                    Console.Out.WriteLine("Learn Ontology: Não te apanhei!!");
                        getSuperClass((Entity)statement.Object, ontology);
                }     
            }
        }

        public void mergeOntology(MemoryStore ontology, String concept)
        {
            
            String prefixe = ((Tag)localOntology.Ontologia[((List<String>)localOntology.Sujeitos())[0]]).Prefix;            

            foreach (Entity entity in ontology.GetEntitiesOfType("http://www.w3.org/2000/01/rdf-schema#Class"))
            {
                Tag tag = Ontology.Ontology.parseUri(entity.Uri.ToLower());

                if (tag.Concept.ToLower().Equals(concept.ToLower()))
                {
                    
                    this.localOntology.Store.Add(new Statement(entity, (Entity)"http://www.w3.org/2000/01/rdf-schema#Type", (Entity)"http://www.w3.org/2000/01/rdf-schema#Class"));
                    getSuperClass(entity, ontology);
                    break;
                }
                
            }

            smartBrowser.GUI.SmartBrowser.Instance.Ontologia.SaveOntology();

        }

        private String deleteBlankSpaces(String word)
        {                      
            return word.Replace(' ', '_');
        }

    }
}

